package com.hgy.crowd.service.impl;

import com.hgy.crowd.config.OSSProperties;
import com.hgy.crowd.service.ProjectService;
import com.hgy.crowd.util.CrowdUtil;
import com.hgy.crowd.util.ResultEntity;
import com.hgy.crowd.vo.ProjectVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author He
 * @Date 2020/7/4 21:19
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private OSSProperties ossProperties;

    @Override
    public ResultEntity uploadHeaderPicture(MultipartFile multipartFile, ProjectVO projectVO) {
        if (!multipartFile.isEmpty()) {
            try {
                ResultEntity<String> uploadHeaderPicResultEntity = CrowdUtil.uploadFileToOSS(
                        ossProperties.getEndPoint(),
                        ossProperties.getAccessKeyId(),
                        ossProperties.getAccessKeySecret(),
                        multipartFile.getInputStream(),
                        ossProperties.getBucketName(),
                        ossProperties.getBucketDomain(),
                        multipartFile.getOriginalFilename());
                if (ResultEntity.SUCCESS.equals(uploadHeaderPicResultEntity.getResult())) {
                    String headerPicturePath = uploadHeaderPicResultEntity.getData();
                    projectVO.setHeaderPicturePath(headerPicturePath);
                    return ResultEntity.successWithData(projectVO);
                } else {
                    return ResultEntity.failed("图片上传失败，失败原因：" + uploadHeaderPicResultEntity.getMessage());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultEntity.failed(e.getMessage());
            }

        } else {
            return ResultEntity.successWithoutData();
        }
    }

    @Override
    public ResultEntity uploadDetailPicList(List<MultipartFile> multipartFiles, ProjectVO projectVO) {
        List<String> detailPicPaths = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            if (!multipartFile.isEmpty()) {
                try {
                    ResultEntity<String> uploadHeaderPicResultEntity = CrowdUtil.uploadFileToOSS(ossProperties.getEndPoint(),
                            ossProperties.getAccessKeyId(),
                            ossProperties.getAccessKeySecret(),
                            multipartFile.getInputStream(),
                            ossProperties.getBucketName(),
                            ossProperties.getBucketDomain(),
                            multipartFile.getOriginalFilename());
                    if (ResultEntity.SUCCESS.equals(uploadHeaderPicResultEntity.getResult())) {
                        String headerPicturePath = uploadHeaderPicResultEntity.getData();
                        detailPicPaths.add(headerPicturePath);
                    } else {
                        return ResultEntity.failed("图片上传失败，失败原因：" + uploadHeaderPicResultEntity.getMessage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ResultEntity.failed(e.getMessage());
                }
            } else {
                return ResultEntity.successWithoutData();
            }

        }
        projectVO.setDetailPicturePathList(detailPicPaths);
        return ResultEntity.successWithoutData();
    }

    @Override
    public ResultEntity uploadReturnPic(MultipartFile returnPicture) {
        if (!returnPicture.isEmpty()){
            try {
                ResultEntity<String> uploadHeaderPicResultEntity = CrowdUtil.uploadFileToOSS(ossProperties.getEndPoint(),
                        ossProperties.getAccessKeyId(),
                        ossProperties.getAccessKeySecret(),
                        returnPicture.getInputStream(),
                        ossProperties.getBucketName(),
                        ossProperties.getBucketDomain(),
                        returnPicture.getOriginalFilename());
                return uploadHeaderPicResultEntity;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultEntity.failed(e.getMessage());
            }
        }else {
            return ResultEntity.failed("请选中图片后再进行上传");
        }
    }
}
